package com.java.thread.demo.chapter1.interrupt;

/**
 * Created by yiyongyao on 2017/12/19.
 */
public class MyThreadInterrupt extends Thread {

/*    @Override
    public void run() {


        for (int i = 0; i < 50000; i++) {
            //isInterrupted() 判断线程是否已经中断
            if (this.isInterrupted()) {
                System.out.println("已经是停止状态,我要退出了!");
                break;
                //throw new InterruptedException();
            } else {
                System.out.println("i = " + (i + 1));
            }
        }
        System.out.println("线程虽然中断了,但我还是会执行, 如不想执行请在for中抛出 exception,放弃使用break;");


    }*/


    @Override
    public void run() {


        try {
            for (int i = 0; i < 50000; i++) {
                //isInterrupted() 判断线程是否已经中断
                if (this.isInterrupted()) {
                    System.out.println("已经是停止状态,我要退出了!");
                    //break;
                    throw new InterruptedException();
                } else {
                    System.out.println("i = " + (i + 1));
                }
            }
            System.out.println("线程虽然中断了,但我还是会执行, 如不想执行请在for中抛出 exception,放弃使用break;");
            System.out.println("线程是否停止过: " + this.isInterrupted());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
